﻿
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Scrum.Net.Classes;
using Scrum.Net.Interfaces;
using Scrum.Net.Utilities;
using Scrum.Net.Utilities.Commands;

namespace Scrum.Net.ViewModels
{
    public class ProjectViewModel : ObservableObject, IViewModel
    {
        private readonly IProject _project;

        public ServiceManager Service;

        //public bool IsProjectLoaded;

        private RelayCommand _newStoryCommand;
        private RelayCommand _deleteStoryCommand;
        private RelayCommand _newUserCommand;
        private RelayCommand _deleteUserCommand;

        private readonly ObservableCollection<IScrumRole> _roles;

        public ObservableCollection<IScrumRole> Roles
        {
            get { return _roles; }
        }

        public IUser SelectedProjectUser { get; set; }
        public IProductStory SelectedProjectBacklog { get; set; }

        #region Commands
        public ICommand NewUserCommand
        {
            get { return _newUserCommand ?? (_newUserCommand = new RelayCommand(NewUser, () => Service.IsProjectLoaded)); }
        }

        public ICommand DeleteUserCommand
        {
            get { return _deleteUserCommand ?? (_deleteUserCommand = new RelayCommand(DeleteUser, () => SelectedProjectUser != null)); }
        }

        public ICommand NewStoryCommand
        {
            get { return _newStoryCommand ?? (_newStoryCommand = new RelayCommand(NewStory, () => Service.IsProjectLoaded)); }
        }

        public ICommand DeleteStoryCommand
        {
            get { return _deleteStoryCommand ?? (_deleteStoryCommand = new RelayCommand(DeleteStory, () => SelectedProjectBacklog != null)); }
        }
        #endregion

        public void NewUser()
        {
            IUser user = new User("New User");
            user.Id = "U1";
            user.Role = new ScrumRole("SrumMaster");

            _project.Users.Add(user);
        }

        public void DeleteUser()
        {
            if (SelectedProjectUser != null)
            {
                _project.Users.Remove(SelectedProjectUser);
            }
        }

        public ProjectViewModel(IProject project, ServiceManager service)
        {
            _project = project;
            Service = service;

            //Service.Messenger.Register("ChangeProject", (Action<bool>)(param => IsProjectLoaded = param));

            _roles = new ObservableCollection<IScrumRole>();
            _roles.Add(new ScrumRole("SrumMaster"));
            _roles.Add(new ScrumRole("TeamMember"));
        }

        private void NewStory()
        {
            IProductStory story = new ProductStory("new Story");
            _project.Backlog.Add(story);
        }

        private void DeleteStory()
        {
            if (SelectedProjectBacklog != null)
            {
                _project.Backlog.Remove(SelectedProjectBacklog);
            }
        }
    }
}
